package com.songlanyun.modules.keywordsearch.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.modules.exception.SearchException;
import com.songlanyun.modules.keywordsearch.entity.SystemKeyWordSearch;
import org.apache.commons.lang.StringUtils;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.keywordsearch.dao.SystemKeyWordSearchDao;
import com.songlanyun.modules.keywordsearch.service.SystemKeyWordSearchService;


@Service("systemKeyWordSearchService")
public class SystemKeyWordSearchServiceImpl extends ServiceImpl<SystemKeyWordSearchDao, SystemKeyWordSearch> implements SystemKeyWordSearchService {

    @Autowired
    private RedissonClient redissonClient;

    @Override
    public SystemKeyWordSearch create(SystemKeyWordSearch entity) {
        this.save(entity);
        return entity;
    }

    @Override
    public SystemKeyWordSearch modify(SystemKeyWordSearch entity) {
        this.updateById(entity);
        return entity;
    }

    @Override
    public void remove(Long id) {

    }

    @Override
    public Boolean saveKeyWord(String keyWord) {
        if(StrUtil.isEmpty(keyWord)){
            throw new RRException(SearchException.KEY_WORD_NOT_NULL);
        }
        if(keyWord.length()>50)
            throw new RRException(SearchException.KEY_WORD_TOO_LONG);

        SystemKeyWordSearch search = this.getOne(new LambdaQueryWrapper<SystemKeyWordSearch>()
                .eq(SystemKeyWordSearch::getKeyWord, keyWord).last("limit 1")
        );
        if(null == search){
            search = new SystemKeyWordSearch(keyWord);
            this.create(search);
        }else{
            search.addKeyWordSearchNum();
            this.updateById(search);
        }
        return Boolean.TRUE;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SystemKeyWordSearch> page = this.page(
                new Query<SystemKeyWordSearch>().getPage(params),
                new LambdaQueryWrapper<SystemKeyWordSearch>()
                        .like(StringUtils.isNotBlank(MapUtil.getStr(params,"keyword")),
                                SystemKeyWordSearch::getKeyWord,
                                MapUtil.getStr(params,"keyword"))
        );
        return new PageUtils(page);
    }

    @Override
    public List<SystemKeyWordSearch> loadList(Integer limit) {
        List<SystemKeyWordSearch> list = this.list(new LambdaQueryWrapper<SystemKeyWordSearch>()
                .orderByDesc(SystemKeyWordSearch::getTotalNum)
                .last("limit " + limit));
        return list;
    }

}